package com.wanglemei.edm.model;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import com.wanglemei.util.EmailUtils;

public class Email implements Serializable {
    private static final long    serialVersionUID = 3970991328238243752L;

    public static final String[] MIAGE_SERVER_IDS = new String[] { "d1", "d2", "d3", "d4", "s2", "s3" };

    private String               toEmailAddress;
    private String               subject;//主题
    private String               content;//内容
   

    private Map<String, String>  params           = new HashMap<String, String>();



    public Email() {
    }



    public Email(String toEmailAddress, String subject, String content) {
        this.toEmailAddress = toEmailAddress;
        this.subject = subject;
        this.content = content;
    }



    public Boolean validate() {
        if (EmailUtils.isBlankString(this.toEmailAddress) || EmailUtils.isBlankString(this.subject)
                || EmailUtils.isBlankString(this.content)) {
            return false;
        }

        if (!EmailUtils.validateEmailAddress(this.toEmailAddress)) {
            return false;
        }

        return true;
    }



    public void addParam(String key, String value) {
        this.params.put(key, value);
    }



    public String getSubject() {
        String cursubject = populateWithParameters(subject, this.params);

        return cursubject;
    }


    /**
     * 替换模板中的
     * @param orgStr
     * @param paramters
     * @return
     */
    public String populateWithParameters(String orgStr, Map<String, String> paramters) {
        if (orgStr == null || orgStr.trim().length() == 0) {
            return "";
        }
        if (paramters != null) {
            for (String key : paramters.keySet()) {
                String val = paramters.get(key);
                if (!EmailUtils.isBlankString(key) || !EmailUtils.isBlankString(val)) {
                    try {
                        orgStr = orgStr.replace("{" + key + "}", val);
                    } catch (Exception e) {
                        System.out.println("ERROR. while execute populateWithParameters()");
                        System.out.println("orgStr=" + orgStr);
                        System.out.println("key=" + key);
                        System.out.println("val=" + val);
                        System.out.println(e);
                    }
                }
            }
        }

        return orgStr;
    }



    public void setSubject(String subject) {
        this.subject = subject;
    }



    public String getContent() {
        try {
            Integer imgSvrInd = Double.valueOf(Math.floor(Math.random() * MIAGE_SERVER_IDS.length)).intValue();
            String curContent = content.replaceAll("\\{imgbase\\}", MIAGE_SERVER_IDS[imgSvrInd]);
            curContent = populateWithParameters(curContent, this.params);
            return curContent;
        } catch (NullPointerException e) {
            return null;
        }
    }



    public void setContent(String content) {
        this.content = content;
    }



    public void setToEmailAddress(String toEmailAddress) {
        this.toEmailAddress = toEmailAddress;
    }



    public String getToEmailAddress() {
        return toEmailAddress;
    }



    public void setParams(Map<String, String> params) {
        this.params = params;
    }



    public Map<String, String> getParams() {
        return params;
    }



    @Override
    public String toString() {
        return toString(false);
    }

 

	public String toString(boolean containContent) {
        String toString = "Email [toEmailAddress=" + toEmailAddress + ", subject=" + subject;
        if (containContent) {
            toString = toString + ", content=" + content;
        }
        toString = toString + ", params=" + params + "]";
        return toString;
    }
}
